package com.hctforgreen.greenservice.service;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.baidu.mapapi.SDKInitializer;
import com.hctforgreen.greenservice.db.DBCityManager;
import com.hctforgreen.greenservice.db.DbAdapter;
import com.hctforgreen.greenservice.db.DbColumnAdapter;
import com.hctforgreen.greenservice.db.DbEntryAdapter;
import com.hctforgreen.greenservice.db.DbExamAdapter;
import com.hctforgreen.greenservice.db.DbFeedBackAdapter;
import com.hctforgreen.greenservice.db.DbSearchKeywordAdapter;
import com.hctforgreen.greenservice.db.DbTerminalExamAdapter;
import com.hctforgreen.greenservice.model.ExamSubEntity;
import com.hctforgreen.greenservice.utils.ApplicationCrashHandleUtil;
import com.hctforgreen.greenservice.utils.LocationServiceUtil;

import java.io.File;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * @author Tallen Zhai
 */
public class HctApplication extends Application {
    //390778  增加百分比
    public static int percent;

    private DbAdapter dbAdapter;
    private DbExamAdapter dbExamAdapter;

    private DbSearchKeywordAdapter dbKeywordAdapter;
    private DbColumnAdapter dbColumnAdapter;
    private DbEntryAdapter dbEntryAdapter;
    private DbFeedBackAdapter dbFeedBackAdapter;

    public static List<ExamSubEntity> examSubEntities = null;

    /**
     * 终端培训：考试
     */
    public static List<ExamSubEntity> terminalExamSubEntities = null;
    private LocationServiceUtil locationServiceUtil;

    private DBCityManager dbm;
    private SQLiteDatabase locationdb;

    /**
     * 2016-9-20、390694
     * 终端培训 考试
     */
    private DbTerminalExamAdapter dbTerminalExamAdapter;



    @Override
    public void onCreate() {
        super.onCreate();
        /**
         * DB底层操作，程序生命周期内仅在此初始化一次
         */
        //防止重复创建
        if (dbAdapter == null) {
            dbAdapter = new DbAdapter(this);
        }
        /**
         * 获取省市区数据库，程序生命周期内仅在此初始化一次
         */
        dbm = new DBCityManager(this);
        dbm.openDatabase();
        locationdb = dbm.getDatabase();

/*		*//**
         * 获取系列机型数据库，程序生命周期内仅在此初始化一次
         *//*


        //=======================================
        dbExamAdapter = new DbExamAdapter(this);
        dbTerminalExamAdapter = new DbTerminalExamAdapter(this);
        dbKeywordAdapter = new DbSearchKeywordAdapter(this);
        dbColumnAdapter = new DbColumnAdapter(this);
        dbEntryAdapter = new DbEntryAdapter(this);
        dbFeedBackAdapter = new DbFeedBackAdapter(this);
        locationServiceUtil = new LocationServiceUtil(getApplicationContext());
        JPushInterface.setDebugMode(false); // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);

        /******====初始化崩溃日志的获取=====20161107 390697*****/
        // 文件路径设置
        String parentPath = "";
        String fileName = "hct";
        // 存在SDCARD的时候，路径设置到SDCARD
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            parentPath = Environment.getExternalStorageDirectory().getPath() + File.separator + fileName;
            // 不存在SDCARD的时候，路径设置到ROM
        } else {
            parentPath = Environment.getDataDirectory().getPath() + "/data/" + fileName;
        }

        String logFile = parentPath + "/temp";
        File file = new File(logFile);
        if (!file.exists()) {
            new File(logFile).mkdirs();
        }
        ApplicationCrashHandleUtil crashHandleUtil = ApplicationCrashHandleUtil.getInstance();
        crashHandleUtil.init(getApplicationContext());
        crashHandleUtil.setLogPath(logFile);
        SDKInitializer.initialize(this);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        dbAdapter.close();
        dbExamAdapter.close();
        dbKeywordAdapter.close();
        dbColumnAdapter.close();
        dbEntryAdapter.close();
        dbFeedBackAdapter.close();
        dbTerminalExamAdapter.close();
    }

    public DbAdapter getDbAdapter() {
        return dbAdapter;
    }

    public DbExamAdapter getDbExamAdapter() {
        return dbExamAdapter;
    }

    public DbTerminalExamAdapter getDbTerminalExamAdapter() {
        return dbTerminalExamAdapter;
    }

    public DbSearchKeywordAdapter getDbKeywordAdapter() {
        return dbKeywordAdapter;
    }

    public DbColumnAdapter getDbColumnAdapter() {
        return dbColumnAdapter;
    }

    public DbEntryAdapter getDbEntryAdapter() {
        return dbEntryAdapter;
    }

    public DbFeedBackAdapter getDbFeedBackAdapter() {
        return dbFeedBackAdapter;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }



    public LocationServiceUtil getLocationServiceUtil() {
        return locationServiceUtil;
    }

    public SQLiteDatabase getLocationdb() {
        return locationdb;
    }

}
